package com.jfinal.weixin.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.msg.in.event.EventInMsg;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.jfinal.weixin.sdk.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MbVote extends Model<MbVote> {
    public static final MbVote dao = new MbVote();
    /**
     * 获取指定选手在指定日期中的增长量
     * @param userid
     * @param date
     * @return
     */
    public int getTodayDes(String userid,String date){
        String sqlstr = "SELECT COUNT(*) FROM `mbvote` where concat(?,' 00:00:00') < ctime and concat(?,' 23:59:59') > ctime and userid=?";
        long mycount = Db.queryLong(sqlstr, new Object[] { date, date,
                userid });
        return (int)mycount;
    }

    /**
     * 今日所有投票次数
     *
     * @return
     */
    public int gettodayall(String openid) {

        String sqlstr = "SELECT COUNT(*) FROM `mbvote` where concat(CURDATE(),' 00:00:00') < ctime and concat(CURDATE(),' 23:59:59') > ctime and secno=?";
        long mycount = Db.queryLong(sqlstr, new Object[] {openid });
        return (int) mycount;
    }
    //
    public int getthism(int userid) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:");
        String nowtime = sdf.format(now);
        String sqlstr = "SELECT COUNT(*) FROM `mbvote` where concat(?,'00') < ctime and concat(?,'59') > ctime and  userid=?";
        long mycount = Db.queryLong(sqlstr, new Object[] { nowtime, nowtime,
                userid });
        return (int) mycount;
    }
    /**
     * 如果填写第三个参数，那么查询为单个用户的次数
     *
     * @param openid
     * @param userid
     * @return
     */
    public int gettodayall(String openid, int userid) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime = sdf.format(now);
        String sqlstr = "SELECT COUNT(*) FROM `mbvote` where concat(CURDATE(),' 00:00:00') < ctime and concat(CURDATE(),' 23:59:59') > ctime and secno=? and userid=?";
        long mycount = Db.queryLong(sqlstr, new Object[] {openid, userid });
        return (int) mycount;
    }

    /**
     * 此人所有投票次数
     *
     * @parRam openid
     * @return
     */
    public int getAllCut(String openid) {
        String sqlstr = "SELECT COUNT(*) FROM `mbvote` where secno=? ";
        long mycount = Db.queryLong(sqlstr, new Object[] { openid });
        return (int) mycount;
    }

    public int getScoreOfSomeOne(int userid){
        String sqlstr = "SELECT id FROM `mbvote` where userid=? and statu=0";
        int a  = Db.find(sqlstr,new Object[] { userid }).size();
        return a;
    }

    public int getallvote(){
        return super.find("select id from mbvote").size();
    }
}
